<?php
//页面显示
class PageAction extends BaseAction {
	var $daoP;
	
	function _initialize(){
		parent::_initialize();
		
		$this->_initChannelId();
		
		$this->daoP = D( 'Page' );
	}
	
	function saveLayout(){
		$layout = $_POST['layout'];
		dump(json_decode($layout));
	}	
	
	function index(){
		$this->admin_tree();
		$this->_myChannel();		
		$userLevel = $this->_isAllow();
		$this->getCategory($userLevel);
		
		if(isset($_GET['pageId'])){
			$id = $_GET['pageId'];
			 $map['id'] = isNumber($id)?
								$id:
								D('Page')->where("domain='".$id."'")->getField('id');
			$temp = $this->daoP->where($map)->find();
			$this->assign( 'page', $temp );
		}

		//频道
		unset($map);
		$map['id'] = $this->channelId;
		$channel = D('Channel', 'portal')->where($map)->find();
		$this->assign('channelInfo', $channel);	
				
		unset($map);
		$map['channelId'] = $this->channelId;
		$uids = D('Purview', 'portal')->where($map)->field('uid')->findall();
		foreach ($uids as $vo){
			$uidArr[] = $vo['uid'];
		}
		$this->assign('manager', $uidArr);		
		
		$this->display();
	}	
	
	function doDealPage(){
		if(empty($_POST['pageId'])){
			$this->doAddPage();
		}else{
			$this->doEditPage();
		}
	}
	
	function doAddPage(){
		$name = t($_POST['name']);
		if(empty($name)){
			$this->error('页面标题不能为空！');
		}
				
		$dao = $this->daoP->create();
		$dao->name = $name;
		$id = $this->daoP->add();		

		if( $id ){			
			$this->success('增加成功！');
		}else {
			$this->error('增加失败！');
		}
	}
		
	function doEditPage(){
		$map['id'] = intval($_POST['pageId']);
		if(empty($map['id'])){
			$this->error('系统参数出错！');
		}
		$name = t($_POST['name']);
		if(empty($name)){
			$this->error('页面标题不能为空！');
		}
		
		$dao = $this->daoP->create();
		$dao->name = $name;//dump($dao);exit;
		$res = $this->daoP->where($map)->save();
		//dump($_POST['type']);	
		//dump($this->daoP->getLastSql());exit;		
		if( $res ){
			$this->success('修改成功！');
		}else {
			$this->error('修改失败！');
		}		
	}	

	function doDelete(){
		$map['id'] = intval($_REQUEST['id']);	
		$res = $this->daoP->where($map)->delete();	
		if($res){
			U('portal/Page/index', 'channelId='.$this->channelId, true);
		}else{
			$this->error('删除失败');
		}		
	}
	
	function checkDomain(){
		$map['channelId'] = $_POST['channelId'];
		$map['domain']    = $_POST['domain'];
		
		$id = $this->daoP->where($map)->getField('id');
		$this->_EchoRes($id);
	}
	
	function category(){
		$this->admin_tree();
		$this->_myChannel();
		$userLevel = $this->_isAllow();
		if($userLevel==1){
			$this->echoRes(false,'您没有权限进行分层管理');
		}
				
		$map['channelId'] = $this->channelId;
		$list = D('PageCategory')->where($map)->findall();
		$this->assign('CategoryList', $list);

		$this->display();
	}
	function doAddCategory(){
		$map['channelId'] = $_POST['channelId'];
		$map['title']    = t($_POST['title']);
		
		$res = D('PageCategory')->add($map);
		$this->echoRes($res,'增加分层');
	}
	function editCategory(){
		$map['id'] = $_GET['id'];
		$cate = D('PageCategory')->where($map)->find();		
		$this->assign('cate', $cate);

		$this->display();		
	}
	function doEditCategory(){
		$map['id'] = $_POST['id'];
		$data['title']    = t($_POST['title2']);	
		$res = D('PageCategory')->where($map)->save($data);
		
		$this->echoRes($res,'修改');
	}
	function deleteCategory(){
		$map2['id'] = $_GET['id'];		
		$res = D('PageCategory')->where($map2)->delete();	
		if($res){
			$map['categoryId'] = $map2['id'];
			$this->daoP->where($map)->setFiled('categoryId', 0);
		}
		$this->echoRes($res, '删除');		
	}
	function addAdmin(){
		$this->assign('id', $_GET['id']);

		$this->display();		
	}	
	function doAddAdmin(){			
		$map['id'] = $_POST['id'];	
		$cate = D('PageCategory')->where($map)->find();
		
		if(empty($cate['admin'])){
			$data['admin'] = $_POST['admin_uids'];
		}else{
			$data['admin'] = $cate['admin'].','.$_POST['admin_uids'];
		}
		$res = D('PageCategory')->where($map)->save($data);
		
		$uids = explode(',', $data['admin']);
		unset($data);
		$data['categoryId'] = $map['id'];
		$data['channelId'] = $this->channelId;
		foreach ($uids as $uid){
			$data['uid'] = $uid;
			if(!empty($uid)){
				$res = D('Purview')->add($data);
			}
		}
		
		$this->echoRes($res,'增加管理员');		
	}
	function delAdmin(){
		$uid = $_GET['uid'];
		$map['id'] = $_GET['id'];	
		$cate = D('PageCategory')->where($map)->field('admin')->find();
		
		if($cate['admin']==$uid){
			$data['admin'] = '';
		}else{
			$uids = explode(',', $cate['admin']);
			$arr = array($uid);
			$uids = array_diff($uids, $arr);
			$data['admin'] = implode(',', $uids);
		}
		$res = D('PageCategory')->where($map)->save($data);
		
		unset($map);
		$map['uid'] = $uid;
		$map['categoryId'] = $_GET['id'];
		$map['channelId'] = $this->channelId;

		$res = D('Purview')->where($map)->delete();	
		//dump(D('Purview')->getLastSql());dump($res);exit;
		$this->echoRes($res,'删除管理员');
	}
	//导出浏览统计
    function export(){
		$pageList = $this->admin_tree();
		$userLevel = $this->_isAllow();
		$cateArr = $this->getCategory($userLevel);
				
		$dataArr = array(0=>array('分层','页面','浏览量'));
		foreach ($cateArr as $key=>$vo){
			$list = $pageList[$key];
			foreach($list as $k=>$v){
				$arr[0] = $vo;
				$arr[1] = $v['name'];
				$arr[2] = $v['readerCount'];
				
			    $dataArr[] = $arr;	
			}
		}
		
        require ADDON_PATH.'/libs/phpExcel/out-csv.php';
        export_csv($dataArr);
    }
}
?>